package com.jiang.smartcommunity.ui.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.jiang.smartcommunity.R;
import com.jiang.smartcommunity.tool.Tool;

import java.util.ArrayList;


public class S3DActivity extends BaseActivity implements View.OnClickListener {

    private EditText editTextTextPersonName;
    private Button button2;
    private ImageView imageView8;
    Bitmap bitmap;
    Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_s3_d);
        initView();
        setTitle("发图文状态");
    }

    private void initView() {
        editTextTextPersonName = (EditText) findViewById(R.id.editTextTextPersonName);
        button2 = (Button) findViewById(R.id.button2);
        imageView8 = (ImageView) findViewById(R.id.imageView8);

        button2.setOnClickListener(this);

        imageView8.setOnClickListener(v -> {
            try {
                startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"), 888);
            } catch (Exception e) {
                toast(e.getMessage());
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        try {
            uri = data.getData();
           // bitmap = (Bitmap) data.getExtras().get("data");
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
            imageView8.setImageBitmap(bitmap);
        } catch (Exception e) {
            toast(e.getMessage());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button2:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String editTextTextPersonNameString = editTextTextPersonName.getText().toString().trim();
        if (TextUtils.isEmpty(editTextTextPersonNameString)) {
            Toast.makeText(this, "文字不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (bitmap == null) {
            Toast.makeText(this, "图片不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        Tool.beans.add(new S3Activity.Bean("住户301", editTextTextPersonNameString, 0, R.drawable.tx3, bitmap, new ArrayList<>()));
        toast("发布成功");

        finish();

    }
}